/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */
package com.yami.shop.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.app.dto.UserInfoDto;
import com.yami.shop.bean.model.User;
import com.yami.shop.bean.model.UserExtension;
import com.yami.shop.bean.param.MemberReqParam;
import com.yami.shop.bean.param.MemberTrendRespParam;
import com.yami.shop.common.util.Arith;
import com.yami.shop.common.util.DateParam;
import com.yami.shop.common.util.DateUtils;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.dao.UserExtensionMapper;
import com.yami.shop.dao.UserMapper;
import com.yami.shop.service.UserExtensionService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 用户扩展信息
 *
 * @author LGH
 * @date 2020-02-26 16:03:14
 */
@Service
@AllArgsConstructor
public class
UserExtensionServiceImpl extends ServiceImpl<UserExtensionMapper, UserExtension> implements UserExtensionService {

    private final UserExtensionMapper userExtensionMapper;
    private final UserMapper userMapper;

    @Override
    public Page<UserExtension> getPage(PageParam<UserExtension> page, UserExtension userExtension) {
        return userExtensionMapper.getPage(page,userExtension);
    }

    @Override
    public void setUserLevelBylevelId(Integer level, Integer levelType) {
        userExtensionMapper.setUserLevelBylevelId(level,levelType);
    }

    @Override
    public void setMaxUserLevelByLevelId(Integer level, Integer minNeedGrowth, Integer maxNeedGrowth, Integer levelType) {
        userExtensionMapper.serMaxUserLavelByLevelId(level,minNeedGrowth,maxNeedGrowth,levelType);
    }

    @Override
    public void updateBatchByUserId(List<UserExtension> userExtensions) {
        userExtensionMapper.updateBatchByUserId(userExtensions);
    }

    @Override
    public List<MemberTrendRespParam> getMemberTrend(MemberReqParam param) {
        List<MemberTrendRespParam> resList = new ArrayList<>();
        Date startTime = param.getStartTime();
        Date endTime = param.getEndTime();
        List<DateParam> everyDays = DateUtils.findEveryDays(startTime, endTime);
        int total = 0;
        for (DateParam everyDay : everyDays) {
            MemberTrendRespParam res = new MemberTrendRespParam();
            param.setDateTime(null);
            param.setStartTime(everyDay.getStartTime());
            param.setEndTime(everyDay.getEndTime());
            res.setCurrentDay(DateUtils.dateToNumber(everyDay.getStartTime()));
            res.setMemberNum(userExtensionMapper.countMemberByParam(param));
            total = total + res.getMemberNum();
            resList.add(res);
        }
        for (MemberTrendRespParam respParam : resList) {
            respParam.setMemberNumRate(divAverage(respParam.getMemberNum(),total,4));
        }
        return resList;
    }

    @Override
    public void updateBalanceByVersion(UserExtension userExtension) {
        userExtensionMapper.updateBalanceByVersion(userExtension);
    }

    @Override
    public UserInfoDto getUserInfo(String userId) {
        UserInfoDto userInfoDto = userExtensionMapper.getUserInfo(userId);
        return userInfoDto;
    }

    @Override
    public void updateUserScoreOrGrowth(List<String> userIds, Integer score, Integer growth) {
        if (Objects.isNull(score) && Objects.isNull(growth)) {
            return;
        }
        //增加/减少积分或者成长值 , sql中处理积分和成长值不能为负数，最小为0
        userExtensionMapper.updateUserScoreOrGrowth(userIds, score, growth);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMemberByTime(List<User> userList, List<UserExtension> userExtensions) {
        userMapper.updateBatchById(userList);

        updateBatchById(userExtensions);
    }


    private Double divAverage(Integer a, Integer b,Integer scale) {
        if (Objects.isNull(b) || b == 0 || Objects.isNull(a) || a == 0) {
            return 0.0;
        }
        return Arith.div(a,b,scale);
    }
}
